//引入路径
import config from './config'
import {
  showToast
} from './tips'
//封装请求函数
export default function({url,method='get',data={}}){
  const header = {
    'Content-Type':'application/json'
  }
  const token = wx.getStorageSync('token')
  token && (header.token = token)
  return new Promise((resolve,reject) => {
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: config.baseUrl+url,
      method,
      data,
      header,
      success:res => {
        wx.hideLoading()
        if(res.data.code === 200){
          resolve(res.data)
        }else{
          showToast(res.data.data,'error')
          reject(res.data.data)
        }    
      },
      fail:err => {
        wx.hideLoading()
        showToast('请求失败')
        reject(err)
      }
    })
  })
}